<?php
/**
 * (jobs)
 */
namespace App\Jobs\AmazonApi\SpApi;

use App\Logic\Amazon\Api\Report;
use App\Logic\Amazon\Api\RequiredConfig;
use App\Models\Basics\AmazonAccount;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class GrabGetReports implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 2;

    /**
     * 任务运行的超时时间。
     *
     * @var int
     */
    public $timeout = 235;

    protected $account;
    protected $report_types;
    protected $marketplace_ids;
    protected $created_since;
    protected $created_until;

    /**
     * GrabGetReports constructor.
     * @param AmazonAccount $account
     * @param $report_types  []
     * @param $marketplace_ids []
     * @param $created_since
     * @param $created_until
     */
    public function __construct(AmazonAccount $account,$report_types,$marketplace_ids,$created_since,$created_until)
    {
        $this->account = $account;
        $this->report_types = $report_types;
        $this->marketplace_ids = $marketplace_ids;
        $this->created_since = $created_since;
        $this->created_until = $created_until;
    }

    /**
     * @throws \Exception
     */
    public function handle()
    {
        $requiredConfig = RequiredConfig::getInit();
        $apiConfig = $requiredConfig->configuration($this->account->refresh_token,$this->account->aws_region);
        Report::apiGetReports($apiConfig,$this->account,$this->report_types,['DONE'],$this->marketplace_ids,10,$this->created_since,$this->created_until);
    }
}
